package JDBCUI;

import  jdbc.util.JdbcUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Huode extends JFrame {


    JLabel jlpno = new JLabel("教师     编号：");
    JTextField jtpno = new JTextField("",20);
    JLabel jlnum = new JLabel("获得奖惩编号：");
    JTextField jtnum = new JTextField("",20);

    JButton buttonadd = new JButton("添加");

    JButton buttonreturn = new JButton("返回");

    JButton buttondelete = new JButton("删除某位教师具体记录");
    JButton buttonbrowse = new JButton("浏览教师编号和奖惩编号");
    JButton buttonbrowse2 = new JButton("查看总情况");
    JButton buttonalldeletel3 = new JButton("删除教师全部奖惩记录");








    public Huode() {

        JPanel jppno = new JPanel();
        JPanel jpnum = new JPanel();

        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpnum.add(jlnum);
        jpnum.add(jtnum);

        jpforbutton.add(buttonadd);
        jpforbutton.add(buttonreturn);
        jpforbutton.add(buttonbrowse);
        jpforbutton.add(buttondelete);
        jpforbutton.add(buttonbrowse2);
        jpforbutton.add(buttonalldeletel3);




        buttonadd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String xpno=jtpno.getText();
                System.out.println(xpno);
                String xnum = jtnum.getText();

                    Connection con =null;
                    PreparedStatement ps = null;
                    ResultSet resultSet = null;

                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    con= DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");

                    Statement statement = con.createStatement();
                    resultSet = statement.executeQuery("select allstaff.pno,staff.pno,allstaff.name from allstaff,staff where allstaff.pno='"+xpno+"' and staff.pno='"+xnum+"'   ");

                    if (!resultSet .next()){
                        if(e.getSource()==buttonadd){
                            javax.swing.JOptionPane.showMessageDialog(null, "不存在，添加失败！重新添加");
                        }
                    }
                    resultSet = statement.executeQuery("select allstaff.pno,staff.pno,allstaff.name from allstaff,staff where allstaff.pno='"+xpno+"' and staff.pno='"+xnum+"'   ");


                    while (resultSet.next()) {

                        String sql2 = "INSERT INTO huode(pno,getnum,name)" + "values(?,?,?)";
                        ps = con.prepareStatement(sql2);

                        ps.setInt(1, resultSet.getInt("allstaff.pno"));
                        ps.setInt(2, resultSet.getInt("staff.pno"));
                        ps.setString(3, resultSet.getString("allstaff.name"));

                        ps.executeUpdate();

                            javax.swing.JOptionPane.showMessageDialog(null, "添加成功!");

                    }
                } catch (Exception ex) {

                    if(e.getSource()==buttonadd){
                        javax.swing.JOptionPane.showMessageDialog(null, "添加失败！！！重新添加！添加操作结束!");
                    }

                }finally {

                    JdbcUtil.colse(con,ps,resultSet);

                }




            }
                });



        buttonreturn.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                dispose();
            }

        });


        buttonbrowse.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Huodebrowse huodebrowse =new Huodebrowse();
            }
        });
        buttondelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Huodedelete huodedelete = new Huodedelete();
            }
        });


        buttonbrowse2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Huodeallbrowse hb=  new Huodeallbrowse();
            }
        });
buttonalldeletel3.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {

        Huodealldelete hd = new Huodealldelete();

    }
});


        this.setTitle("添加 教师 奖惩编号");
        this.setLayout(new GridLayout(9,1));
        this.add(jppno);
        this.add(jpnum);

        this.add(jpforbutton);
        this.setLocation(800,300);
        this.setSize(1000,400);
        this.setVisible(true);


    }
}
